<template>
  <div class="app-container">
    <el-button type="success" icon="el-icon-finished" style="margin: 10px;" @click="handleSubmit">
      提交
    </el-button>
    <el-button type="info" icon="el-icon-refresh" style="margin: 10px;" @click="handleRefresh">
      重置
    </el-button>
    <el-button type="info" icon="el-icon-circle-close" style="margin: 10px;" @click="handleClose">
      关闭
    </el-button>
    <el-button type="danger" icon="el-icon-delete" style="margin: 10px;" @click="handleDelete">
      删除
    </el-button>

    <el-form ref="channelEditForm" :rules="channelRules" :model="channel" label-width="100px" style="margin-left:0px;">
      <el-form-item label="渠道名称" prop="name">
        <el-input v-model="channel.name" style="width: 400px;" />
      </el-form-item>
      <el-form-item label="渠道简称" prop="shortName">
        <el-input v-model="channel.shortName" style="width: 400px;" />
      </el-form-item>
      <el-form-item label="渠道介绍" prop="description">
        <el-input v-model="channel.description" type="textarea" :rows="4" style="width: 400px;" />
      </el-form-item>
      <el-form-item label="网关账号" prop="relatedGatewayAccountId">
        <el-select v-model="channel.relatedGatewayAccountId" clearable>
          <el-option v-for="item in gatewayAccountOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="库存更新时间" prop="inventoryUpdateTime">
        <el-date-picker
          v-model="channel.inventoryUpdateTime"
          type="datetime"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder=""
        />
      </el-form-item>
      <el-form-item label="库存更新备注" prop="inventoryUpdateRemark">
        <el-input v-model="channel.inventoryUpdateRemark" style="width: 400px;" />
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { getSupplierById, putSupplierById, deleteSupplierById } from '@/api/manager/channel-api'
import { toOptionsList } from '@/utils/index'

export default {
  name: 'ManagerChannelSupplierEdit',
  filters: {
  },
  data() {
    return {
      channel: {
        id: null,
        name: null,
        shortName: null,
        description: null,
        relatedGatewayAccountId: null,
        inventoryUpdateTime: null,
        inventoryUpdateRemark: null
      },
      channelRules: {
        name: [{
          required: true, message: '请输入渠道名称', trigger: 'blur'
        }],
        shortName: [{
          required: true, message: '请输入渠道简称', trigger: 'blur'
        }]
      }
    }
  },
  async created() {
    await this.initLoad()
    await this.loadDetail()
  },
  methods: {
    async initLoad() {
      const dataDictsParam = ['SUPPLIER_GATEWAY_ACCOUNT']
      const { data } = await fetchEnum(dataDictsParam)
      for (const dataDict of data) {
        if (dataDict.code === 'SUPPLIER_GATEWAY_ACCOUNT') {
          if (dataDict.options) {
            this.gatewayAccountOptions = toOptionsList(dataDict.options, 'id', 'username')
          }
        }
      }
    },
    loadDetail() {
      getSupplierById(this.$route.params.id).then(response => {
        const { data } = response
        this.channel = data
      })
    },

    handleSubmit() {
      this.$refs.channelEditForm.validate((valid) => {
        if (!valid) {
          return
        }
        var data = JSON.parse(JSON.stringify(this.channel))
        putSupplierById(this.channel.id, data).then(response => {
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 1 * 1000
          })
        }).catch(error => {
          Message({
            message: error,
            type: 'error',
            duration: 5 * 1000
          })
        })
      })
    },
    handleRefresh() {
      this.loadDetail()
    },
    handleClose() {
      this.$store.dispatch('tagsView/delView', this.$route)
      this.$router.go(-1)
    },
    handleDelete() {
      deleteSupplierById(this.channel.id).then(response => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 1 * 1000
        })
        this.handleClose()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    }

  }
}

</script>
